#pragma once
#include<vector>
#include<functional>
#include<iostream>
using namespace std;
namespace bit
{
    template <class T, class Container = vector<T>, class Compare = less<T> >
    class priority_queue
    {
    public:

        priority_queue(){}
        void AdjustDown(int parent) 
        {
            int n = c.size();
            for (int child = parent * 2 + 1; child < n; parent = child, child = parent * 2 + 1)
            {
                if (child + 1 < n && comp(c[child],c[child+1]))
                    child++;
                if (comp(c[parent], c[child]))
                    swap(c[parent], c[child]);
                else
                    break;
            }
        }

        void AdjustUp(int child)
        {
            for (int parent = (child - 1) / 2; child > 0; child = parent, parent = (child - 1) / 2)
            {
                if (comp(c[parent], c[child]))
                    swap(c[parent], c[child]);
                else
                    break;
            }
        }

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
            :c(first,last)
        {
            for (int i = (c.size() - 1 - 1) / 2; i >= 0; i--)
                AdjustDown(i);
        }

        bool empty() const
        {
            return c.empty();
        }

        size_t size() const
        {
            return c.size();
        }

        const T& top() const
        {
            return c[0];
        }

        void push(const T& x)
        {
            c.push_back(x);
            AdjustUp(c.size() - 1);
        }

        void pop()
        {
            swap(c.back(), c[0]);
            c.pop_back();
            AdjustDown(0);
        }

    private:
        Container c;
        Compare comp;
    };
};